﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1.mysqlunit
{
    class sqlstring
    {
        List<intobean> infobeans = new List<intobean>();
        List<List<intobean>> datainfobeans = new List<List<intobean>>();
        List<String> wheres = new List<string>();
        string DUPLICATE = "";
        //添加where条件
        public void addwhere(string where)
        {
            wheres.Add(where);
        }
        //初始化写入多条容器
        public void addinfos(List<intobean> infos)
        {
            datainfobeans.Add(infos);
        }
        //添加写入的词组
        public void addinfo(String key ,String value)
        {
            if (value == "")
            {
                value = null;
            }
            var infobean = new intobean(key,value);
            infobeans.Add(infobean);
        }
        //插入的sql
        public string instersql(string table)
        {
            var sql = "INSERT INTO "+table;
            for (int i = 0;i< datainfobeans[0].Count;i++)
            {
                var bean = datainfobeans[0][i];
                if(i == 0)
                {
                    sql = sql + " (";
                }
                sql = sql + bean.key;
                if(i == datainfobeans[0].Count - 1)
                {
                    sql = sql + ")";
                }
                else
                {
                    sql = sql + ",";
                }
            }
            sql = sql + "VALUES";
            for (int j = 0; j < datainfobeans.Count; j++)
            {
                var beans = datainfobeans[j];
                for (int i = 0; i < beans.Count; i++)
                {
                    var bean = beans[i];
                    if (i == 0)
                    {
                        sql = sql + " (";
                    }
                    if (bean.value == null)
                    {
                        sql = sql + "null";
                    }
                    else
                    {
                        sql = sql + "\"" + bean.value + "\"";
                    }

                    if (i == beans.Count - 1)
                    {
                        sql = sql + ")";
                    }
                    else
                    {
                        sql = sql + ",";
                    }
                }
                if (j < datainfobeans.Count - 1)
                {
                    sql = sql + ",";
                }
            }
            if(DUPLICATE != "")
            {
                sql = sql + sqlDuplicate();
            }
            sql = sql + ";";
            return sql;
        }
        //更新的sql
        public string updatesql(string table)
        {
            var sql = "UPDATE "+table + " SET";
            for (int i=0;i<infobeans.Count();i++)
            {
                var into = infobeans[i];
                sql = sql + " " + into.key + " = '" + into.value+"'";
                if (i< infobeans.Count()-1)
                {
                    sql = sql + ",";
                }
            } 
            if (wheres.Count>0)
            {
                sql = sql + " WHERE ";
                for (int i = 0; i< wheres.Count; i++) {
                    sql = sql + wheres[i];
                    if (i < wheres.Count - 1)
                    {
                        sql = sql + " AND ";
                    }
                }
               
            }
            sql = sql + ";";
            return sql;
        }
        //删除的sql
        public string deletesql(string table)
        {
            var sql = "DELETE " + table + " SET";
 
            if (wheres.Count > 0)
            {
                sql = sql + " WHERE ";
                for (int i = 0; i < wheres.Count; i++)
                {
                    sql = sql + wheres[i];
                    if (i < wheres.Count - 1)
                    {
                        sql = sql + " AND ";
                    }
                }

            }
            sql = sql + ";";
            return sql;
        }
        //新增是没有则更新
        public void addDuplicate(string DUPLICATE)
        {
            this.DUPLICATE = DUPLICATE;
        }
        public string sqlDuplicate()
        {
            var sql = "on DUPLICATE key update "+DUPLICATE;
            return sql;
        }
        //读出数据的sql
        public string selectsql()
        {
            return "";
        }
    }
}
